package org.csu.jpetmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetmanage.mapper.ItemMapper;
import org.csu.jpetmanage.mapper.ProductMapper;
import org.csu.jpetmanage.model.Category;
import org.csu.jpetmanage.model.Item;
import org.csu.jpetmanage.model.Order;
import org.csu.jpetmanage.model.Product;
import org.csu.jpetmanage.service.CategoryService;
import org.csu.jpetmanage.service.ProductService;
//import org.csu.jpetmanage.utills.DataInfo;
import org.csu.jpetmanage.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.management.Query;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {

    /**
     * 小类管理首页
     *
     */

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private  Product product;
    @Autowired
    private  Product product1;
    @Autowired
    private  Category category;
    @Autowired
    private ItemMapper itemMapper;

    /**
     *
     * @paramGet
     * 进入小类管理主页面
     */
    @GetMapping("/product")
    public ModelAndView product(Model model){

        List<Product> productList=productService.queryProductAll();
        List<Category>categoryList=categoryService.list(null);
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("productList",productList);
        return new ModelAndView("product");
    }


    /**
     *
     * @param request
     * 实现分页
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.POST)
    public Object findAll(HttpServletRequest request)
    {
        Integer pageNo=Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize=Integer.valueOf(request.getParameter("pageSize"));
        IPage<Product> page=new Page<>(pageNo,pageSize);
        QueryWrapper<Product>wrapper=new QueryWrapper<>();
        Product product=new Product();
        product.setProductid("1");
        wrapper.setEntity(product);
        return productService.page(page,wrapper);
    }

    /**
     *
     * 实现添加功能
     */
    //@RequestParam("category") String category,
    @RequestMapping("/productAdd")
    public String  productAdd(@RequestParam("productid") String productid,
                              @RequestParam("name") String name,
                              @RequestParam("category") String category,
                              MultipartFile file,
                              Model model,
                              HttpSession session) throws IOException {
        product.setProductid(productid);
        product.setCategory(category);
        product.setName(name);
        System.out.println("文件的内容是");
        System.out.println(file);
        product.setDescn(UploadUtil.uploadImage(file));
        productMapper.insert(product);
        session.setAttribute("msg","插入成功");
        return "redirect:product";
    }

    /**
     *
     * 删除小类
     */
    @GetMapping("/productDelete")
    public ModelAndView delete(@RequestParam("productid") String productid,Model model)
    {
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productid",productid);
        itemMapper.delete(queryWrapper);
        int a = productMapper.deleteById(productid);
        List<Product> productList=productService.queryProductAll();
        model.addAttribute("productList",productList);
        return new ModelAndView("product");
    }

    /**
     *
     * 修改小类
     */
    @GetMapping("/productEditAjax")
    @ResponseBody
    public Map<String,Object> productEditAjax(
            @RequestParam("productid") String productid,
            HttpSession session
    ){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productid);//是根据Order实体类中的属性来进行取值
        List<Product> productList =productMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        if (productList.size()==1){
            System.out.println("orderList size is 1");
            product = productList.get(0);
            session.setAttribute("editProduct",product);//待会处理编辑数据的时候需要用
            //把下面补全
            map.put("productid",product.getProductid());
            map.put("category",product.getCategory());
            map.put("name",product.getName());
//            map.put("descn",product.getDescn());
            return map;
        }
        return map;
    }

    @RequestMapping("/productEdit")
    public String productEdit(
            @RequestParam("editProductid") String productid,
            @RequestParam("editCategory") String category,
            @RequestParam("editName") String name,
            @RequestParam("editFile") MultipartFile file,
            Model model, HttpSession session
    ) throws IOException {
        product.setProductid(productid);
        product.setDescn(UploadUtil.uploadImage(file));
        product.setCategory(category);
        product.setName(name);
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("productid",product.getProductid());
        int rows = productMapper.update(product,updateWrapper);
        System.out.println("Edited rows number is: "+rows);
        session.setAttribute("msg","修改成功");
        return "redirect:/product";
    }

    @RequestMapping("/productSearch")
    public String productSearch(
            @RequestParam("keyword") String keyword,
            Model model){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("productid",keyword).
                or().like("name",keyword).
                or().like("category",keyword).
                or().like("descn",keyword);
        List<Product> productList=productMapper.selectList(queryWrapper);
        model.addAttribute("productList" ,productList);
        List<Category>categoryList=categoryService.list(null);
        model.addAttribute("categoryList",categoryList);
        return "product";
    }

    @GetMapping("/productSearchAjax")
    @ResponseBody
    public List<Product> productSearchAjax(
            @RequestParam("keyword") String keyword,
            HttpSession session){
        System.out.println(keyword+" in OrderController");
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("productid",keyword).
                or().like("name",keyword).
                or().like("category",keyword).
                or().like("descn",keyword);
        return productMapper.selectList(queryWrapper);
    }


}
